package com.sharp.core.utils;

import java.util.ArrayDeque;
import java.util.Queue;

public class Pool<E> {

	Queue<E> freeList;
	Class<E> clazz;
	
	public Pool(Class<E> cl) {
		this.clazz = cl;
		freeList = new ArrayDeque<E>();
	}
	
	public synchronized E aquire() {
		if ( freeList.size() > 0 )
			return freeList.poll();
		try {
			return clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		assert false;
		return null;
	}
	
	public synchronized void release(E obj) {
		freeList.add(obj);
	}
}
